package com.bainuo.doctor.common.image_support.imghandle.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bainuo.doctor.common.R;
import com.bainuo.doctor.common.image_support.imghandle.uploader.UploadTask;
import com.bainuo.doctor.common.util.ViewUtil;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * Created by faraway on 15/2/17.
 */
public class PickerItemViewLoader {


    private Context context;


    public ViewGroup view;
    /**
     * 关闭按钮
     */
    public ImageView closeView;

    /**
     * 重试按钮
     */
    public ImageView retryView;

    /**
     * 进度条
     */
    public SimpleDraweeView maskView;

    /**
     * 图片
     */
    public SimpleDraweeView simpleDraweeView;


    /**
     * loader 索引 在缓存数组中占用的位置
     */
    public int index;

    /**
     * loader 标识  保存了上传任务id
     */
    public String id;


    public PickerItemViewLoader(Context context) {
        this.context = context;
        view = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.item_image_picker, null, false);
        closeView = (ImageView) view.findViewById(R.id.item_picker_close);
        maskView = (SimpleDraweeView) view.findViewById(R.id.item_picker_mask);
        retryView = (ImageView) view.findViewById(R.id.item_picker_retry);
        simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.item_picker_img);
    }

    private boolean rounded = true;

    /**
     * 设置圆角
     */
    private void setMaskRound(boolean round) {
        if (round == rounded)
            return;

        if (!rounded) {
            //四个角都为圆角
            float[] corners = maskView.getHierarchy().getRoundingParams().getCornersRadii();
            maskView.getHierarchy().setRoundingParams(RoundingParams.fromCornersRadii(corners[7], corners[7], corners[7], corners[7]));
        } else {
            //左上与右上 应该设置为无圆角
            float[] corners = maskView.getHierarchy().getRoundingParams().getCornersRadii();
            maskView.getHierarchy().setRoundingParams(RoundingParams.fromCornersRadii(0, 0, corners[7], corners[7]));
        }
        rounded = round;
    }


    /**
     * 设置进度条
     */
    public void setProgress(int progress) {

        setMaskRound(progress < 5);
//        maskView.setAspectRatio(((float)progress)/100);
        //设置进度高度
        if (maskView.getWidth() != 0)
            ViewUtil.setViewHeight(maskView, maskView.getWidth() * (100 - progress) / 100);
    }


    /**
     * 设置状态
     */
    public void setState(int state) {

        closeView.setVisibility(View.VISIBLE);

        if (state == UploadTask.STATE_FAILED) {
            //上传失败 显示重试
            retryView.setVisibility(View.VISIBLE);
            ViewUtil.setViewHeight(maskView, maskView.getWidth());
            setMaskRound(true);
        } else if (state == UploadTask.STATE_COM) {

            //设置完成状态
            retryView.setVisibility(View.GONE);
            maskView.setVisibility(View.GONE);
        } else {

            //设置正在下载状态
            retryView.setVisibility(View.GONE);
            maskView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 重置所有状态
     */
    public void reset() {
        retryView.setVisibility(View.GONE);
        maskView.setVisibility(View.GONE);
        closeView.setVisibility(View.GONE);
        id = null;
    }

}

